﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.Shop.ChargeCard
{
    public partial class ChargeCardList : System.Web.UI.Page
    {
        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

            QueryFilterList filters = new QueryFilterList();

            if (!string.IsNullOrEmpty(Request["code"]))
            {
                filters.Add(new StringFilter("TicketCode", "%" + Request["code"] + "%", StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(Request["member"]))
            {
                filters.Add(new StringFilter("MemberAccount", "%" + Request["member"] + "%", StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(Request["ruleid"]))
            {
                filters.Add(new NumberFilter("RuleId", Int32.Parse(Request["ruleid"])));
            }
            if (!string.IsNullOrEmpty(Request["status"]) && Request["status"] != "-1")
            {
                filters.Add(new StringFilter("Status", Request["status"]));
            }
            filters.Add(new StringFilter("CouponType", "chargecard"));

            string hql = "from ShopCouponTicket where 1=1 ";

            foreach (DataFilter filter in filters)
            {
                hql += " and " + filter.GetSection();
            }

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                IQuery query = session.CreateQuery("select count(*) " + hql);
                filters.SetUp(query);
                //设置记录总数
                _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());

                query = session.CreateQuery(hql);
                filters.SetUp(query);
                filters.SetPager(query, pageIndex, pageSize);

                //设置数据源
                repeater1.DataSource = query.List<ShopCouponTicket>();
            }

            repeater1.DataBind();
        }
    }
}